cmake_minimum_required(VERSION 3.16)
project(ChatServerMock LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 一次性查找所有需要的 Qt6 组件
find_package(Qt6 6.5 REQUIRED COMPONENTS
    Core
    Widgets
    HttpServer
    WebSockets
    Protobuf
)

# 检查 proto 文件是否存在
if(NOT PB_FILES)
    file(GLOB PB_FILES "../ChatClient/proto/*.proto")
endif()

# 如果没有找到 proto 文件，可以设置默认值或报错
if(NOT PB_FILES)
    message(WARNING "No proto files found at ../ChatClient/proto/*.proto")
endif()

qt_standard_project_setup()

qt_add_executable(ChatServerMock
    WIN32 MACOSX_BUNDLE
    main.cpp
    widget.cpp
    widget.h
    widget.ui
    httpserver.h httpserver.cpp
    websocketserver.h websocketserver.cpp
    debug.h
    resource.qrc
)

# 如果有 proto 文件才添加 protobuf 支持
if(PB_FILES)
    qt_add_protobuf(ChatServerMock PROTO_FILES ${PB_FILES})
endif()

# 一次性链接所有依赖库
target_link_libraries(ChatServerMock PRIVATE
    Qt6::Core
    Qt6::Widgets
    Qt6::Network
    Qt6::WebSockets
    Qt6::Protobuf
    Qt6::HttpServer
)

include(GNUInstallDirs)

install(TARGETS ChatServerMock
    BUNDLE  DESTINATION .
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
)

qt_generate_deploy_app_script(
    TARGET ChatServerMock
    OUTPUT_SCRIPT deploy_script
    NO_UNSUPPORTED_PLATFORM_ERROR
)
install(SCRIPT ${deploy_script})
